import axios from 'axios'

// 二. 创建 axios 的实例
const instance = axios.create({
  baseURL: 'http://127.0.0.1:3000',
  timeout: 3000, //超时时间，超时则请求失败，单位为毫秒
})

// 三. 配置拦截器

// 给instance实例配置响应拦截器(统一处理HTTP响应)
// 分请求拦截器和响应拦截器
// 这里使用请求拦截器
instance.interceptors.request.use(
  config => {
    if (localStorage.token) {
      // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.Authorization = `Bearer ${localStorage.token}`
    }
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

instance.interceptors.response.use(
  // 成功的回调
  function (response) {
    // 直接返回 后端的数据
    return response.data
  },
  // 失败的回调
  function (error) {
    return Promise.reject(error)
  }
)

/* 封装validate请求  validate本质上是 post  */
/*  之后可以在组件中 使用 import {validate} from 来导入 */
export const validate = (url, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

/* 封装login请求  login本质上是 post  */
/*  之后可以在组件中 使用 import {validate} from 来导入 */
export const login = (url, data, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url,
      data,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

/* 封装发布公告的路由 */
export const httpPostNote = (url, data, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url,
      data,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

/* 封装获取用户的路由 */
export const getUsers = (url, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'get',
      url,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

/* 封装根据旧的用户名，修改新用户名和新密码 */
export const updatePassword = (url, data, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'put',
      url,
      data,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

/* 封装根据用户名删除用户 */
export const deluserbyusername = (url, data, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'delete',
      url,
      data,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}
/* 封装新增一个交管用户 */
export const addTrafficUser = (url, data, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url,
      data,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

/* 改变登录状态的方法 */
export const changeOnline = (url, data, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'put',
      url: `/online?${url}`,
      data,
      ...config,
    })
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}
